<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\StoreProject as StoreProjectModel;

/**
 * 门店项目接口
 */
class Project extends Api
{

    protected $noNeedRight = ['*'];

    //项目列表
    public function lists()
    {
        $store_id = input('store_id',0);
        if (empty($store_id)){
            $this->error('缺少参数门店id');
        }

        $list = StoreProjectModel::where(['store_id'=>$store_id])->select();
        $this->success('请求成功',$list);
    }

    //添加项目
    public function add()
    {
        $model = new StoreProjectModel();

        $data = [
            'store_id'=>input('store_id',0),
            'project_name'=>input('project_name',''),
            'hour'=>input('hour',0),
            'minute'=>input('minute',0),
            'price'=>input('price',0)
        ];
        if (empty($data['store_id'])){
            $this->error('缺少参数门店id');
        }
        if (empty($data['project_name'])){
            $this->error('请输入项目名称');
        }
        if (empty($data['hour'])&&empty($data['minute'])){
            $this->error('请输入项目时长');
        }
        if (empty($data['price'])){
            $this->error('请输入项目金额');
        }
        $res = $model->save($data);
        if ($res){
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }

    //编辑项目
    public function edit()
    {
        $project_id = input('project_id',0);
        if (empty($project_id)){
            $this->error('缺少参数项目id');
        }

        $model = new StoreProjectModel();

        $data = [
            'store_id'=>input('store_id',0),
            'project_name'=>input('project_name',''),
            'hour'=>input('hour',0),
            'minute'=>input('minute',0),
            'price'=>input('price',0)
        ];
        if (empty($data['store_id'])){
            $this->error('缺少参数门店id');
        }
        if (empty($data['project_name'])){
            $this->error('请输入项目名称');
        }
        if (empty($data['hour'])&&empty($data['minute'])){
            $this->error('请输入项目时长');
        }
        if (empty($data['price'])){
            $this->error('请输入项目金额');
        }
        $res = $model->save($data,['project_id'=>$project_id]);
        if ($res){
            $this->success('编辑成功');
        } else {
            $this->error('编辑失败');
        }
    }

    //项目详情
    public function info()
    {
        $project_id = input('project_id',0);
        if (empty($project_id)){
            $this->error('缺少参数项目id');
        }

        $info = StoreProjectModel::get($project_id);

        $this->success('请求成功',$info);
    }


    //删除项目
    public function del()
    {
        $project_id = input('project_id',0);
        if (empty($project_id)){
            $this->error('缺少参数项目id');
        }

        $info = StoreProjectModel::destroy($project_id);

        $this->success('请求成功',$info);
    }

}